const MongoClient = require('mongodb').MongoClient;
const HOST = '39.108.173.196'
const PORT = '23454'
const URL = `mongodb://${HOST}:${PORT}`

function dbConnect (databaseName,cb) {
    MongoClient.connect(URL,{ useUnifiedTopology: true }, function(err, client) {
        if(err) {
            console.error(err)
        }else {
            const db = client.db(databaseName);
            cb && cb(db)
        }
    });
}

module.exports.insertOne = ({
    databaseName,
    collection,
    data,
    cb
}) => {
    dbConnect(databaseName,db => {
        db.collection(collection).insertOne(data,function(err,results){
            cb(err,results);
        })
    })
}
module.exports.insertMany= ({
    databaseName,
    collection,
    data,
    cb
}) => {
    _connect(databaseName,(db) => {
        db.collection(collection).insertMany(data,function(err,results){
            cb(err,results);
        })
    })
}
module.exports.find= ({
    databaseName,
    collection,
    data,
    sort,
    limit,
    skip,
    cb
}) => {
    _connect(databaseName,(db) => {
        db.collection(collection).find(data)
        .sort(sort)
        .limit(limit)
        .skip(skip)
        .toArray(function(err,results){
            cb(err,results);
        })
    })
}